define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'speech/speech/index',
                    add_url: 'speech/speech/add',
                    edit_url: 'speech/speech/edit',
                    del_url: 'speech/speech/del',
                    multi_url: 'speech/speech/multi',
                    table: 'speech',
                }
            });
            var table = $("#table");
            $.get("speech/speech/index",function (data) {
                if(data.total>4){
                    $('.btn-add').attr('disabled',true)
                }
            });
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true,visible:false},
                        {field: 'id', title: __('Id')},
                        {field: 'name', title: __('Name')},
                        {field: 'admin_id', title: __('Admin_id'), visible: false},
                        {
                            field: 'industry',
                            title: __('Industry'),
                            visible: false,
                            searchList: {
                                "finance": __('industry finance'),
                                "house": __('industry house'),
                                "edu": __('industry edu'),
                                "ad": __('industry ad'),
                                "other": __('industry other')
                            }
                        },
                        {field: 'industry_text', title: __('Industry'), operate: false},
                        {field: 'admin.nickname', title: __('创建人')},
                        // {field: 'speech.name', title: __('话术名称')},
                        {
                            field: 'createtime',
                            title: __('Createtime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'updatetime',
                            title: __('Updatetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            visible: false,
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'id',
                            title: __('配置话术路径'),
                            formatter: Controller.api.formatter.id
                        },
                        {
                            field: 'id',
                            title: __('配置话术关键词'),
                            formatter: Controller.api.formatter.speech_id
                        },
                        {field: 'admin.id', title: __('Admin.id'), visible: false},
                        {
                            field: 'operate',
                            width: "120px",
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate
                        }
                        // {
                        //     field: 'operate',
                        //     title: __('Operate'),
                        //     table: table,
                        //     buttons: [
                        //         {
                        //             name: "router",
                        //             icon: 'fa fa-list',
                        //             title: __('话术路径'),
                        //             extend: 'data-toggle="tooltip"',
                        //             classname: 'btn btn-xs btn-primary btn-speech-route'
                        //         }
                        //     ],
                        //     events: {
                        //         'click .btn-speech-route': function (e, value, row, index) {
                        //             e.stopPropagation();
                        //             e.preventDefault();
                        //             var table = $(this).closest('table');
                        //             var options = table.bootstrapTable('getOptions');
                        //             var ids = row[options.pk];
                        //             row = $.extend({}, row ? row : {}, {ids: ids});
                        //             var url = '';
                        //             //url = Table.api.replaceurl(url, row, table);
                        //             //url = Fast.api.fixurl(url);
                        //             url = 'speech/router?speech_id=' + row.id;
                        //             //location.href = '../' + url;
                        //             Backend.api.addtabs(url, __('话术路径') + '&nbsp;-&nbsp;' + row.name, 'fa fa-list-alt');
                        //             //Fast.api.open(Table.api.replaceurl(url, row, table), __('话术路径'), $(this).data() || {});
                        //
                        //         }
                        //     },
                        //     formatter: Table.api.formatter.operate
                        //     // formatter: Controller.api.formatter.id
                        // }
                    ]
                ]
            });
            $('.btn-add').on('click', function () {
                $.get("speech/speech/index",function (data) {
                    if(data.total>3){
                        $('.btn-add').attr('disabled',true)
                    }
                });
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },

        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            formatter: {
                id: function (value, row, index) {
                    //这里手动构造URL
                    url = "speech/router?speech_id=" + value;

                    //方式一,直接返回class带有addtabsit的链接,这可以方便自定义显示内容
                    return '<a href="' + url + '" class="label label-success addtabsit" title="' + __("配置话术路径", value) + '">' + __('配置话术路径', value) + '</a>';

                    //方式二,直接调用Table.api.formatter.addtabs
                    //return Table.api.formatter.addtabs(value, row, index, url);
                },
                speech_id: function (value, row, index) {
                    //这里手动构造URL
                    url = "speech/faq?speech_id=" + value;

                    //方式一,直接返回class带有addtabsit的链接,这可以方便自定义显示内容
                    return '<a href="' + url + '" class="label label-success addtabsit" title="' + __("配置话术关键词", value) + '">' + __('配置话术关键词', value) + '</a>';

                    //方式二,直接调用Table.api.formatter.addtabs
                    //return Table.api.formatter.addtabs(value, row, index, url);
                }
            }
        }
    };
    return Controller;
});